<?php

class Push_excel_Controller Extends Controller {

    private $objectlist;

    function _init() {
        check_level(50, segment(0), segment(1, 'index'));
        //header("Content-type:text/html;charset=UTF-8"); 
        //date_default_timezone_set('PRC'); 
        $this->frequence = array('once' => translate('ONCE'),  'now' => translate('RIGHT_NOW'));
        $this->objectlist = get_type_object_id_name();
    }

    function index() {
    	$this->view->assign("frequence", $this->form->select("row[frequence]", $this->frequence, array(), array('id' => 'frequence')));
        $this->view->display("push/push_excel/index");
        set_time_limit(0);
        if(is_submit())
        {
        	$this->clear_tmp_token();
        	//print_r($_FILES);
        	if($_FILES['file']['error']>0)
        	{
        		echo "Error: " . $_FILES["file"]["error"] . "<br />";
        		//return;
        	}
        
        	$filetype   =$_FILES['file']['type'];
        	$tmp_name   =$_FILES['file']['tmp_name'];	//临时文件名
        	$file_name  =$_FILES['file']['name'];	//文件名
        	$awardpath       =MULTIUPLOADPATH.'excel/mail/'.$file_name;
        	 
        	//判断下后缀是否正确
        	//echo " filename=$file_name ";
        	$exfile = substr(strrchr($file_name, '.'), 1);
        	//echo " exfile1 = $exfile ";
        
        	$exfile = strtolower(trim($exfile));
        	//echo " exfile2 = $exfile ";
        
        	if ( 'xls' != $exfile && 'xlsx' != $exfile)
        	{
        		echo " error file should be xls or xlsx ";
        		return;
        	}
        
        	//上传文件
        	//die($awardpath);
        	$ret = move_uploaded_file($tmp_name, $awardpath);
        	if(true != $ret)
        	{
        		echo " error fail to upload file";
        		return;
        	}
        	$rowdata = $_POST['row'];
        	if ($rowdata['frequence'] == 'now') {
        		$err = "";	//错误信息集
        		//$this->load->database("db");
        		$this->db->select_db();
        		$excelarray =  readExcel($awardpath);
        		if (empty($excelarray)) {
        			echo "空数据";
        			return ;
        		}
        		for($i=2;$i<=count($excelarray);$i++)
        		{
        		$awardarray['areaId']            =$excelarray[$i]['A'];	//区号
        		$awardarray['role_name']       =$excelarray[$i]['B'];	//角色名
        		$awardarray['role_id']         =$excelarray[$i]['C'];	//角色id
        		$sql = "insert tmp_token (token) SELECT token FROM tt_role_token WHERE area = '".$awardarray['areaId']."' and roleid in(".$awardarray['role_id'].")";
        		$insertRes = $this->db->query($sql);
        		}
        		include_once 'fsocket.php';
        		$pushByToken = new Push_By_Token_Controller($this->db);
        		$rowdata = $_POST['row'];
        		$pushByToken->push($rowdata['content']['content']);
        		echo "<script>alert('success');</script>";
        	}
        	else {
        		$insertData = array();
        		$insertData['excelfile']  = addslashes($awardpath);
        		$insertData['content']  = $rowdata['content']['content'];
        		$insertData['starttime'] = strtotime($rowdata['starttime']);
        		$insertData['type'] = 3;
        		$insertRes = $this->db->insert('push_message',$insertData);
        		if ($insertRes === false) {
     				echo "<script>alert('fail');</script>";
     			}
        		else {
        			echo "<script>alert('success');</script>";
        		}
        	}
        }
    }
	
    private function clear_tmp_token() {
    	$sql = "truncate table tmp_token";
    	return $this->db->query($sql);
    }
    
}

?>